<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/14
 * Time: 16:18
 */

namespace app\admin\controller;


use app\admin\model\Order;
use app\admin\model\ShopFoods;
use think\Controller;

class TimerController extends Controller
{

    public function cronTab(){
        $this->start();
        $this->end();
        $this->sellOut();

    }
    private function start(){
        $food = ShopFoods::where('spike_status',1)->select()->toArray();
        if ($food){
            foreach ($food as $k=>$v){
                if (strtotime($v['start_time']) <= time()){
                    ShopFoods::update(['spike_status'=>2],['id'=>$v['id']]);
                }
            }
        }

    }

    private function end(){
        $food = ShopFoods::where('spike_status','in',[2,3])->select()->toArray();
        if ($food){
            foreach ($food as $k=>$v){
                if (strtotime($v['end_time']) <= time()){
                    ShopFoods::update(['spike_status'=>4],['id'=>$v['id']]);
                }
            }
        }
    }

    private function sellOut(){
        $food = ShopFoods::where('spike_status',2)->select()->toArray();
        if ($food){
            foreach ($food as $k=>$v){
                if ($v['stock'] == $v['lock']){
                    ShopFoods::update(['spike_status'=>3],['id'=>$v['id']]);
                }
            }
        }
    }




}